package edu.tcc.iot.persistence.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import edu.tcc.iot.domain.Product;
import edu.tcc.iot.domain.ProductInfo;
import edu.tcc.iot.persistence.DBContext;
import edu.tcc.iot.persistence.dao.IProductDAO;
import edu.tcc.iot.utils.DaoUtils;
import edu.tcc.iot.utils.ServiceLocator;

public class ProductDAO implements IProductDAO {

	private DBContext dbContext = ServiceLocator.getInstance(DBContext.class);

	@Override
	public Product findById(long id) throws SQLException {
		Product result = null;
		Connection connection = dbContext.getConnection();
		try {
			String sqlProduct = dbContext.getProductQuery("dao.findby.id");
			PreparedStatement statementProduct = connection.prepareStatement(sqlProduct);
			statementProduct.setLong(1, id);
			ResultSet product = statementProduct.executeQuery();

			if(product.first()){
				String sqlProductInfo = dbContext.getProductInfoQuery("dao.findby.product.id");
				PreparedStatement statementProductInfo = connection.prepareStatement(sqlProductInfo);
				statementProductInfo.setLong(1, id);
				ResultSet productInfo = statementProductInfo.executeQuery();
				productInfo.first();
				result =  DaoUtils.productFromResultSet(product, productInfo);
			}

		}catch (SQLException e) {
			connection.rollback();
		}finally{
			connection.close();
		}
		return result;
	}

	@Override
	public synchronized void add(Product product, long shelfId) throws SQLException {
		Connection connection = dbContext.getConnection();
		try {

			ProductInfo info = product.getProductInfo();
			String sqlProduct = dbContext.getProductQuery("dao.add");
			PreparedStatement statementProduct = connection.prepareStatement(sqlProduct);
			statementProduct.setLong(1, product.id);
			statementProduct.setLong(2, shelfId);
			statementProduct.executeUpdate();

			String sqlProductInfo = dbContext.getProductInfoQuery("dao.add");
			PreparedStatement statementProductInfo = connection.prepareStatement(sqlProductInfo);
			statementProductInfo.setLong(1, product.getId());
			statementProductInfo.setString(2, info.getName());
			statementProductInfo.setDouble(3, info.getQuantity());
			statementProductInfo.setDouble(4, info.getPrice());
			long date = info.getValidate().getTime();
			statementProductInfo.setDate(5, new java.sql.Date(date));
			statementProductInfo.executeUpdate();

		}catch (SQLException e) {
			connection.rollback();
		}finally{
			connection.close();
		}
	}

	@Override
	public synchronized void update(Product entity) throws SQLException {
		Connection connection = dbContext.getConnection();
		try{
			ProductInfo info = entity.getProductInfo();
			String sqlProductInfo = dbContext.getProductInfoQuery("dao.update");
			PreparedStatement statementProductInfo = connection.prepareStatement(sqlProductInfo);
			statementProductInfo.setString(1, info.getName());
			statementProductInfo.setDouble(2, info.getQuantity());
			statementProductInfo.setDouble(3, info.getPrice());
			long date = info.getValidate().getTime();
			statementProductInfo.setDate(4, new java.sql.Date(date));
			statementProductInfo.setLong(5, entity.id);
			statementProductInfo.executeUpdate();
		}catch (SQLException e) {
			connection.rollback();
		}finally{
			connection.close();
		}
	}

	@Override
	public synchronized void remove(long id) throws SQLException {
		Connection connection = dbContext.getConnection();
		try {
			String sqlProduct = dbContext.getProductQuery("dao.remove");
			PreparedStatement statementProduct = connection.prepareStatement(sqlProduct);
			statementProduct.setLong(1, id);
			statementProduct.executeUpdate();
		}catch (SQLException e) {
			connection.rollback();
			e.printStackTrace();
		}
		finally{
			connection.close();
		}
	}
	
	@Override
	public List<Product> findByShelf(long shelfId) throws SQLException{
		List<Product> products = new ArrayList<Product>();
		Connection connection = dbContext.getConnection();
		try{
			String sql = dbContext.getProductQuery("dao.findby.shelf.id");
			PreparedStatement statement = connection.prepareStatement(sql);
			statement.setLong(1, shelfId);
			ResultSet productIds = statement.executeQuery();
			while(productIds.next()){
				products.add(findById(productIds.getLong("id")));
			}
			return products;
		}catch (SQLException e) {
			connection.rollback();
		}finally{
			connection.close();
		}
		return products;
	}
	
	@Override
	public boolean exists(Product product) throws SQLException {
		boolean result = false;
		Connection connection = dbContext.getConnection();
		try{
			String sqlProduct = dbContext.getProductQuery("dao.findby.id");
			PreparedStatement statementProduct = dbContext.getConnection().prepareStatement(sqlProduct);
			statementProduct.setLong(1, product.getId());
			ResultSet resultSet = statementProduct.executeQuery();
			result =  resultSet.first();
		}catch (SQLException e) {
			connection.rollback();
		}finally{
			connection.close();
		}
		return result;
		
	}

}
